//
//  IWantJoingViewController.m
//  MagazineShangejie
//
//  Created by cnbizMedia on 15/9/14.
//  Copyright (c) 2015年 CnbizMedia. All rights reserved.
//

#import "IWantJoingViewController.h"
#import "Util.h"
#import "UMSocial.h"
#import "DiscoveryTableView.h"
@interface IWantJoingViewController (){

    NSMutableArray *userData;
    __weak IBOutlet UITableView *wordTable;
    __weak IBOutlet UITableView *questionTable;
    
    
    NSDictionary *wordDic;
    NSMutableArray *titleArray;
    NSMutableArray *wordArray;
    
    int cellHight;
    
    UITableViewCell *cell ;
    UITableViewCell *questionCell ;
    NSMutableArray  *questionDicArray;
    NSURL *shareURL;
}

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *scrollAnchorView;
@property (weak, nonatomic) IBOutlet UILabel *ProjectNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UIImageView *headViewImage;
@property (weak, nonatomic) IBOutlet UILabel *goingOnLable;
@property (weak, nonatomic) IBOutlet UILabel *CompanyLable;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;


//Activity
@property (weak, nonatomic) IBOutlet UIView *ActivityView;
@property (weak, nonatomic) IBOutlet UILabel *ActivityTiele;
@property (weak, nonatomic) IBOutlet UILabel *ActivityAboutActivity;
@property (weak, nonatomic) IBOutlet UILabel *ActivityTimeLab;
@property (weak, nonatomic) IBOutlet UILabel *ActivityRealTime;



@end

@implementation IWantJoingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    
    //2. 取出数据
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSInteger number = [userDefault integerForKey:@"buttonTagNumber"];
    
    switch (number) {
        case 0:
            self.title = @"论坛";
            break;
            
        case 1:
            self.title = @"游学";
            break;
            
        case 2:
            self.title = @"咨询";
            break;
            
        case 3:
            self.title = @"创投";
            _headView.hidden = YES;
            _ActivityView.hidden = YES;
            break;
            
        default:
            break;
    }

    
    titleArray = @[@"项目介绍",@"核心优势",@"融资进度",@"目前规模",@"未来预期"];
    NSLog(@"titleArray == %@",titleArray);
 
    NSDictionary *lunTanDic = [userDefault objectForKey:@"lunTanDic"];
    
    NSString *idString = [lunTanDic objectForKey:@"id"];
    NSString *catid    = [lunTanDic objectForKey:@"catid"];
    
    NSDictionary*parameter;
    parameter=@{@"catid":catid,@"id":idString};
    
    [[AppAPIClient sharedClient] GET:@"Club/Show" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"operation = =%@",operation);
        shareURL = operation.request.URL;
        NSLog(@"shareURL -- %@",shareURL);

        NSMutableDictionary *result = responseObject[@"data"];
        NSLog(@"luntan-data == %@",result);
        if([result isKindOfClass:[NSNull class]] || [result count] == 0){
            return;
        }
        if([result isKindOfClass:[NSNull class]] || [result count] == 0||[result[@"content"] count]==0){
            return;
        }
 
        wordDic = nil;
        
        wordDic = [[NSDictionary alloc] initWithDictionary:result[@"content"] copyItems:YES ];

        if ([self.title isEqualToString:@"创投"]) {
            //创投
            _ProjectNameLabel.text = [wordDic objectForKey:@"title"];
            _nameLabel.text = [wordDic objectForKey:@"founder"];

        }
        
        //论坛、游学
        else{
        _CompanyLable.text = [wordDic objectForKey:@"title"];
        _nameLabel.text  = [NSString stringWithFormat:@"参与人数:%@人",[wordDic objectForKey:@"number"] ];
        
            _ActivityAboutActivity.text = [NSString stringWithFormat:  @"%@%@",[wordDic objectForKey:@"area"],[wordDic objectForKey:@"address"]  ];
            _ActivityRealTime.text = [wordDic objectForKey:@"activitytime"];
            
            
        //活动状态
        NSString *activitystatus = [wordDic objectForKey:@"activitystatus"];
        switch ([activitystatus integerValue]) {
 
            case 1:
                _goingOnLable.text = @"凑备中";
                break;
                
            case 2:
                _goingOnLable.text = @"进行中";
                break;
                
            case 3:
                _goingOnLable.text = @"已结束";
                
                break;
                
            default:
                break;
        }
            
            
        }
        
        //设置头图的照片
        [_headImage sd_setImageWithURL:[NSURL URLWithString:wordDic[@"thumb"]]];
        [_headViewImage sd_setImageWithURL:[NSURL URLWithString:wordDic[@"thumb"]]];
        
        
        
        /*
         "id": "1",
         "catid": "3671",
         "title": "俱乐部论坛测试数据一",
         "thumb": "http://www.kanshangjie.com/uploadfile/2015/0911/20150911054954864.jpg",
         "inputtime": "1441964932",
         "activitytime": "2015-09-11 17:48:00",
         "activitystatus": "2",
         "number": "10",
         "area": "陕西省汉中市南郑县",
         "content": "<p>这里是活动详情</p>\r\n<p>这里是换行内容</p>\r\n",
         "address": "郑家院子"
         */
        wordArray = [[NSMutableArray alloc] initWithObjects:[wordDic objectForKey:@"description"],[wordDic objectForKey:@"advantage"],[wordDic objectForKey:@"financingprogress"],[wordDic objectForKey:@"scale"],[wordDic objectForKey:@"expect"],nil];
        NSLog(@"wordArray -- %@",wordArray);
        
        questionDicArray = [[NSMutableArray alloc] initWithArray:result[@"answer"] ];
        NSLog(@"answer-questionDicArray == %@",questionDicArray);
        
        [wordTable reloadData];
        [questionTable reloadData];
       
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
 
    [self setRightBarButton];
  
    //找到anchor view的高度约束修改高度
    NSArray *arrs = _scrollAnchorView.constraints;
    NSLog(@"arrs == %@",_scrollAnchorView.constraints);
    
    for (NSLayoutConstraint *attr in arrs) {
        if(attr.firstAttribute == NSLayoutAttributeHeight){

            NSLog(@"self.view.frame.size.height == %f",self.view.frame.size.height);
            attr.constant = self.view.frame.size.height +150;
            attr.constant = 1750;
            NSLog(@"attr.constant == %f",attr.constant);
        }
    }
    //更新父视图约束条件
    [_scrollAnchorView.superview.superview updateConstraints];
    
    //
    wordTable.separatorStyle = NO;
}

- (void)setRightBarButton
{
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"share01"] forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0, 0, 25, 25)];
    [button addTarget:self action:@selector(rightBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItem= rightItem;
}

//分享按钮
- (void)rightBarButtonClicked
{
    NSString *URLString = [NSString stringWithFormat:@"%@",shareURL];
    
    BDSocialShareContent *content = [BDSocialShareContent shareContentWithDescription:@"超百万企业家都在看的商业财经类阅读客户端" url:URLString title:@"看商界"];
    //    [content addImageWithThumbImage:[UIImage imageNamed:@"icon_default_ipad"] imageUrl:self._Dict[@"thumb"]];
    UIImageView*imageview=[[UIImageView alloc]init];
    imageview.image=[UIImage imageNamed:@"icon"];
    [SVProgressHUD dismiss];
    //分享
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:nil
                                      shareText:[NSString stringWithFormat:@"超百万企业家都在看的商业财经类阅读客户端"]
                                     shareImage:[UIImage imageNamed:@"icon"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToQQ,UMShareToQzone,UMShareToWechatTimeline,UMShareToWechatSession,UMShareToTencent,UMShareToEmail,UMShareToSms,nil]
                                       delegate:self];
    
    [UMSocialData defaultData].extConfig.sinaData.urlResource.url = URLString;
    [UMSocialData defaultData].extConfig.qqData.url = URLString;
    [UMSocialData defaultData].extConfig.qzoneData.url = URLString;
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = URLString;
    [UMSocialData defaultData].extConfig.wechatSessionData.url = URLString;
    
    [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline]];

    


    NSLog(@"wordTable.frame == %f",wordTable.frame.size.height);
}

-(void)didSelectSocialPlatform:(NSString *)platformName withSocialData:(UMSocialData *)socialData
{
    if (platformName == UMShareToSina) {
        socialData.shareText = [NSString stringWithFormat:@"超百万企业家都在看的商业财经类阅读客户端%@ " ,[NSString stringWithFormat:@"%@",shareURL]];
    }
    else{
        socialData.shareText = [NSString stringWithFormat:@"超百万企业家都在看的商业财经类阅读客户端 " ];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




#pragma mark - Table view data source
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //    if (section==0) {
    //        return [self tableHeader];
    //    }else{
    return nil;
    //    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([tableView isEqual:wordTable]){
        return 5;
    }
    
    else if ([tableView isEqual:questionTable])
        
    {
        return questionDicArray.count;
    }
    
    return 0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"wordCell" forIndexPath:indexPath];
////    = [self tableView:tableView cellForRowAtIndexPath:indexPath];
//    
   
    
    if ([tableView isEqual:wordTable]){
        NSLog(@"cell height %f",cell.frame.size.height);
        
        return cell.frame.size.height;
    }
    
    else if ([tableView isEqual:questionTable])
        
    {
        return 120;
    }
    
    return 0;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if ([tableView isEqual:wordTable]) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"wordCell" forIndexPath:indexPath];
        
        if (cell==nil) {
            
            cell=[tableView dequeueReusableCellWithIdentifier:@"wordCell"];
            
        }
        
        UILabel*label5=(UILabel*)[cell.contentView viewWithTag:11];
        label5.text = @"项目介绍";
        label5.text = titleArray[indexPath.row];
        
        UILabel*label7=(UILabel*)[cell.contentView viewWithTag:22];
        label7.text = @"详细介绍";
        label7.text = wordArray[indexPath.row];
        label7.numberOfLines = 0;
        [label7 sizeToFit];
        
    //    label7.text = @"agduqwdgaugdwjhdjqhdiqhwduweihfjwfuewfeuwagduqwdgaugdwjhdjqhdiqhwduweihfjwfuewfeuwagduqwdgaugdwjhdjqhdiqhwduweihfjwfuewfeuwagduqwdgaugdwjhdjqhdiqhwduweihfjwfuewfeuwagduqwdgaugdwj";
        
        NSLog(@"label5.text -- %@",label5.text);
        NSLog(@"label7.text -- %@",label7.text);
        
        CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX);
        cell.userInteractionEnabled = NO;
        
        CGSize requiredSize = [label7.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];
        
        CGRect rect = cell.frame;
        rect.size.height = requiredSize.height+100;
    //    label7.frame.size.height = requiredSize.height + 30;
        cellHight = rect.size.height;
        NSLog(@"cellHight -- %d",cellHight);
        cell.frame = rect;
        
        UIImageView *linImage = (UIImageView *)[cell.contentView viewWithTag:33];
        linImage.backgroundColor = [UIColor grayColor];
     
        
        cell.backgroundColor = UIColorFromRGB(0xf5f5f5);
         
        return cell;
       }
    
    
       else if ([tableView isEqual:questionTable])
           
       {
           
           static NSString *identifier = @"questionCell";
           
           questionCell = [tableView dequeueReusableCellWithIdentifier:identifier];
           
           if (!questionCell) {
               
               questionCell=[tableView dequeueReusableCellWithIdentifier:@"questionCell"];
           }
           
           NSDictionary *questionDic = questionDicArray[indexPath.row];
           
           NSLog(@"questionDicArray == %@",questionDicArray);
           NSLog(@"questionDic == %@",questionDic);
           UILabel*label1=(UILabel*)[questionCell.contentView viewWithTag:111];
           label1.text = @"188***21";
           label1.text = [questionDic objectForKey:@"name"];
           
//           reply introduce
           UILabel*label2=(UILabel*)[questionCell.contentView viewWithTag:222];
           label2.text = @"问题";
           label2.text = [questionDic objectForKey:@"introduce"];
           NSLog(@"[questionDic objectForKey:@introduce]%@",[questionDic objectForKey:@"introduce"]);
           label2.numberOfLines = 0;
           [label2 sizeToFit];
           
           UIImageView *view = (UIImageView *)[questionCell.contentView viewWithTag:444];
           view.backgroundColor = [UIColor grayColor];

           UILabel*label4=(UILabel*)[questionCell.contentView viewWithTag:555];
           label4.text = @"回答";
           label4.text = [questionDic objectForKey:@"reply"];
           label4.numberOfLines = 0;
           [label4 sizeToFit];
           NSLog(@"[questionDic objectForKey:@reply]%@",[questionDic objectForKey:@"reply"]);
           
           UILabel*label3=(UILabel*)[questionCell.contentView viewWithTag:333];
           label3.text = @"2015-1-12";
           
           UILabel*label5=(UILabel*)[questionCell.contentView viewWithTag:666];
           label5.text = @"2015-1-12";
 
           NSString *timeString = [questionDic objectForKey:@"addtime"];
           
//           NSLog(@"timeString -- %@",timeString);
//           //得到运动时间，需要转成年月日
//           
//           NSDateFormatter * formatter = [[NSDateFormatter alloc ] init];
//           [formatter setDateFormat:@"YYYY-MM-dd"];
//           NSString *date =  [formatter stringFromDate:[NSDate date]];
//           NSString *timeLocal = [[NSString alloc] initWithFormat:@"%@", date];
//           NSLog(@"timeLocal%@", timeLocal);

//           NSString *timeString = informationDic[@"inputtime"];
           NSLog(@"timeString -- %@",timeString);
           //得到运动时间，需要转成年月日
           NSString *formatString = @"YYYY-MM-dd";
           
           NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
           [dateFormat setDateFormat:formatString];
           NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[timeString intValue] - 28800];
           NSString *dateString =[NSString stringWithFormat:@"%@",[dateFormat stringFromDate:confromTimesp] ];
           NSLog(@"date: %@", dateString);
           
           label3.text = dateString;
           label4.text = dateString;

           
           questionCell.backgroundColor = UIColorFromRGB(0xf5f5f5);
           return questionCell;
       }
    
    
    
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    indexPath.row;
     [tableView deselectRowAtIndexPath:indexPath animated:NO];//点击后不会有阴影
    
//    NSDictionary *lunTanDic = userData[indexPath.row];
//    NSLog(@"lunTanDic == %@",lunTanDic);
//    
//    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
//    [userDefault setObject:lunTanDic forKey:@"lunTanDic"];
//    [userDefault synchronize];//存入文件
//    
//    
//    if ([self.title isEqualToString:@"论坛"]) {
//        [self performSegueWithIdentifier:@"IWanIdentifier" sender:indexPath];
//    }
    
    
}



//提问题
- (IBAction)questionEvent:(id)sender {
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
